package zucc.lcj.android_finalwork.me;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import zucc.lcj.android_finalwork.LoginActivity;
import zucc.lcj.android_finalwork.MyDatabaseHelper;
import zucc.lcj.android_finalwork.R;

public class MyHelpActivity extends AppCompatActivity {
    //    列出帖子
    private List<MyMessage> messagelist = new ArrayList<>();
    private RecyclerView textlist;
    private MyDatabaseHelper dbHelper;

    private int idflag;
    private int helpflag;
    private String id;
    private String helpid;
    Button back,change;

    @SuppressLint("Range")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_help);
        dbHelper = new MyDatabaseHelper(this,"UserStore.db",null,1);

//        列出帖子
        initMessage();//添加信息
        textlist = findViewById(R.id.textRecycleView);
        textlist.setLayoutManager(new LinearLayoutManager(MyHelpActivity.this));
        textlist.setAdapter(new MyHelpAdapter(messagelist,MyHelpActivity.this));

        back = (Button) findViewById(R.id.my_help_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MyHelpActivity.this, MeActivity.class);
                startActivity(intent);
            }
        });

        change = (Button) findViewById(R.id.my_help_change);
        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final EditText inputId = new EditText(MyHelpActivity.this);
                final EditText inputServer = new EditText(MyHelpActivity.this);
                AlertDialog.Builder builder = new AlertDialog.Builder(MyHelpActivity.this);
                builder.setMessage("输入要修改的帖子编号").setView(inputId)
//                        .setMessage("输入帮助你的用户的名称").setIcon(R.drawable.help_active).setView(inputServer)
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        id = inputId.getText().toString();
                        idflag=0;//0表示不存在，1表示存在
                        helpid = inputServer.getText().toString();
                        helpflag=0;//0表示不存在，1表示存在
                        //判断id是否存在
                        SQLiteDatabase db = dbHelper.getWritableDatabase();
                        String sql = "select * from Help where help_userPost="+LoginActivity.global_login_user_id+" and help_id="+Integer.valueOf(id)+" and help_state='"+"未完成"+"'";
                        Cursor cursor = db.rawQuery(sql,null);
                        if (cursor.moveToFirst()){
                            idflag=1;
                        }
                        if (idflag==0){
                            Toast.makeText(getApplicationContext(), "请输入正确的未完成的帖子编号", Toast.LENGTH_SHORT).show();
                        }
                        if (idflag==1){
                            AlertDialog.Builder builder = new AlertDialog.Builder(MyHelpActivity.this);
                            builder.setMessage("输入帮助你的用户的名称").setIcon(R.drawable.help_active).setView(inputServer)
//                        .setMessage("输入帮助你的用户的名称").setIcon(R.drawable.help_active).setView(inputServer)
                                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                        }
                                    });
                            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    String helpid = inputServer.getText().toString();
                                    helpflag=0;//0表示不存在，1表示存在
                                    //判断用户是否存在且不能是自己
                                    String sql1 = "select * from UserInfo where user_name='"+helpid+"'";
                                    Cursor cursor1 = db.rawQuery(sql1,null);
                                    if (cursor1.moveToFirst()){
                                        helpflag=1;
                                    }
                                    if (helpflag==0){
                                        Toast.makeText(getApplicationContext(), "请输入正确的用户名称", Toast.LENGTH_SHORT).show();
                                    }
                                    if (helpid.equals(LoginActivity.global_login_user)){
                                        Toast.makeText(getApplicationContext(), "不能输入自己的昵称哦", Toast.LENGTH_SHORT).show();
                                        helpflag=0;
                                    }
                                    //全部输入正确 修改帖子状态 修改帮助人的帮助次数
                                    if (idflag==1&&helpflag==1){
                                        SQLiteDatabase db = dbHelper.getWritableDatabase();
                                        String sql2 = "update Help set help_state='已完成' where help_id="+Integer.valueOf(id);
                                        db.execSQL(sql2);
                                        String sql3 = "update UserInfo set helpNum=helpNum+1 where user_name='"+helpid+"'";
                                        db.execSQL(sql3);
                                        Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent();
                                        intent.setClass(MyHelpActivity.this, MeActivity.class);
                                        startActivity(intent);
                                    }
                                }
                            });
                            builder.show();
                        }
                    }
                });
                builder.show();
//                if (idflag==1&&helpflag==1){
//                    SQLiteDatabase db = dbHelper.getWritableDatabase();
//                    String sql2 = "update Help set help_state='已完成' where help_id="+Integer.valueOf(id);
//                    db.execSQL(sql2,null);
//                    String sql3 = "update UserInfo set helpNum=helpNum+1 where user_name="+helpid;
//                    db.execSQL(sql3,null);
//                    Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();
//                    Intent intent = new Intent();
//                    intent.setClass(MyHelpActivity.this, MeActivity.class);
//                    startActivity(intent);
//                }
            }
        });

    }

    @SuppressLint("Range")
    private void initMessage(){
        String title,content,state,photo,userPost,userHelp,time,ids;
        int id;
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql = "select * from Help where help_userPost="+LoginActivity.global_login_user_id;
        Cursor cursor = db.rawQuery(sql,null);
        if (cursor.moveToFirst()){
            do{
                id = cursor.getInt(cursor.getColumnIndex("help_id"));
                ids=String.valueOf(id);
                title=cursor.getString(cursor.getColumnIndex("help_title"));
                title="           "+title;
                content=cursor.getString(cursor.getColumnIndex("help_content"));
                state=cursor.getString(cursor.getColumnIndex("help_state"));
                photo=cursor.getString(cursor.getColumnIndex("help_photo"));
                String sql1="select * from UserInfo where user_id="+ LoginActivity.global_login_user_id;
                Cursor cursor1 = db.rawQuery(sql1,null);
                cursor1.moveToFirst();
                userPost=cursor1.getString(cursor1.getColumnIndex("user_name"));
                time=cursor.getString(cursor.getColumnIndex("help_time"));
                zucc.lcj.android_finalwork.me.MyMessage test=new zucc.lcj.android_finalwork.me.MyMessage(title,content,state,userPost,time,photo,ids);
                messagelist.add(test);
            }while (cursor.moveToNext());
        }
    }
}

